package com.shinechain.service.user.impl;

import com.shinechain.dao.BlackKeyListMapper;
import com.shinechain.dao.WhiteKeyListMapper;
import com.shinechain.dao.WorkBotChatMapper;
import com.shinechain.dao.WorkBotInfoMapper;
import com.shinechain.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

@Service(value = "workBotService")
public class WorkBotService {
    @Autowired
    BlackKeyListMapper blackKeyListMapper;

    @Autowired
    WhiteKeyListMapper whiteKeyListMapper;

    @Autowired
    WorkBotChatMapper workBotChatMapper;

    @Autowired
    WorkBotInfoMapper workBotInfoMapper;

    private boolean getFieldValueByFieldName(String fieldName, Object object) {
        try {
            Field field = object.getClass().getDeclaredField(fieldName);
            //设置对象的访问权限，保证对private的属性的访问
            field.setAccessible(true);
            return  (boolean)field.get(object);
        } catch (Exception e) {
            return false;
        }
    }
    //23个属性
    public void updateWorkBotInfo(WorkBotInfo workBotInfo) {
        //用反射，用每次遍历botMethod的名字，来找workBotInfo中相同的变量的值
        for (Map.Entry<String, Boolean> entry : BotMethodSwitch.botMethod.entrySet()) {
            BotMethodSwitch.botMethod.put(entry.getKey(),this.getFieldValueByFieldName(entry.getKey().toLowerCase(), workBotInfo));
        }
        System.out.println(BotMethodSwitch.botMethod.toString());
        BotMethodSwitch.sendMsgOnTimeStr = workBotInfo.getSendmsgontimestr();
        BotMethodSwitch.sendMsgOnTime = workBotInfo.getSendmsgontime();
        BotMethodSwitch.sendMsgStartMin = workBotInfo.getSendmsgstartmin();
        BotMethodSwitch.sendMsgTimes = workBotInfo.getSendmsgtimes();
        //token
        BotMethodSwitch.token = workBotInfo.getToken();
        BotMethodSwitch.tokenDayTimes = workBotInfo.getTokendaytimes();
        BotMethodSwitch.candy = workBotInfo.getCandy();
        //welcome
        BotMethodSwitch.welcome = workBotInfo.getWelcome();

        //redPaper
        BotMethodSwitch.amount = workBotInfo.getAmount();
        BotMethodSwitch.tokenName = workBotInfo.getTokenname();
        BotMethodSwitch.rewardTotal = workBotInfo.getRewardtotal();

        //checkIn
        BotMethodSwitch.checkInToken = workBotInfo.getCheckintoken();
        BotMethodSwitch.chatToken = workBotInfo.getChattoken();
        BotMethodSwitch.chatDayTimes = workBotInfo.getChatdaytimes();
    }

    public void updateDaoWorkBotInfo(){
        WorkBotInfo workBotInfo = workBotInfoMapper.selectByPrimaryKey(1);
        workBotInfo.setDeletefile(BotMethodSwitch.botMethod.get("deleteFile"));
        workBotInfo.setDeletephoto(BotMethodSwitch.botMethod.get("deletePhoto"));
        workBotInfo.setDeleteadkeyflag(BotMethodSwitch.botMethod.get("deleteAdKeyFlag"));
        workBotInfo.setDeleteforward(BotMethodSwitch.botMethod.get("deleteForward"));
        workBotInfo.setDeletejoinoutmsg(BotMethodSwitch.botMethod.get("deleteJoinOutMsg"));
        workBotInfo.setDeletelongusername(BotMethodSwitch.botMethod.get("deleteLongUsername"));
        workBotInfo.setDeletesendlink(BotMethodSwitch.botMethod.get("deleteSendLink"));
        workBotInfo.setDeletevideo(BotMethodSwitch.botMethod.get("deleteVideo"));
        workBotInfo.setDeletevoice(BotMethodSwitch.botMethod.get("deleteVoice"));
        workBotInfo.setDisableaddbot(BotMethodSwitch.botMethod.get("disableAddBot"));
        workBotInfo.setDisablespam(BotMethodSwitch.botMethod.get("disableSpam"));
        workBotInfo.setKitmanwhodeletethreemsg(BotMethodSwitch.botMethod.get("kitManWhoDeleteThreeMsg"));
        workBotInfo.setUsechat(BotMethodSwitch.botMethod.get("useChat"));
        workBotInfo.setUsetuling(BotMethodSwitch.botMethod.get("useTuling"));
        workBotInfo.setUsewelcome(BotMethodSwitch.botMethod.get("useWelcome"));
        workBotInfo.setUsesendmsgontime(BotMethodSwitch.botMethod.get("useSendMsgOnTime"));
        workBotInfo.setUsetoken(BotMethodSwitch.botMethod.get("useToken"));
        workBotInfo.setUsewhitelist(BotMethodSwitch.botMethod.get("useWhiteList"));
        workBotInfo.setUsetokensay(BotMethodSwitch.botMethod.get("useTokenSay"));
        workBotInfo.setUseredpaper(BotMethodSwitch.botMethod.get("useRedPaper"));
        workBotInfo.setUseguessing(BotMethodSwitch.botMethod.get("useGuessing"));
        workBotInfo.setUsekitman(BotMethodSwitch.botMethod.get("useKitMan"));
        workBotInfo.setUsecheckin(BotMethodSwitch.botMethod.get("useCheckIn"));
        workBotInfo.setUsechattoken(BotMethodSwitch.botMethod.get("useChatToken"));

        workBotInfo.setSendmsgontimestr(BotMethodSwitch.sendMsgOnTimeStr);
        workBotInfo.setSendmsgontime(BotMethodSwitch.sendMsgOnTime);
        workBotInfo.setSendmsgstartmin(BotMethodSwitch.sendMsgStartMin);
        workBotInfo.setSendmsgtimes(BotMethodSwitch.sendMsgTimes);
        //token
        workBotInfo.setToken(BotMethodSwitch.token);
        workBotInfo.setTokendaytimes(BotMethodSwitch.tokenDayTimes);

        //welcome
        workBotInfo.setWelcome(BotMethodSwitch.welcome);

        //redPapper
        workBotInfo.setAmount(BotMethodSwitch.amount);
        workBotInfo.setTokenname(BotMethodSwitch.tokenName);
        workBotInfo.setRewardtotal(BotMethodSwitch.rewardTotal);

        //chatToken,checkIn
        workBotInfo.setChattoken(BotMethodSwitch.chatToken);
        workBotInfo.setChatdaytimes(BotMethodSwitch.chatDayTimes);

        workBotInfo.setCheckintoken(BotMethodSwitch.checkInToken);

        workBotInfoMapper.updateByPrimaryKey(workBotInfo);
    }
    public void updateChatList(List<WorkBotChat> workBotChatList) {
        if (workBotChatList != null && workBotChatList.size() >= 1) {
            for (int i = 0; i < workBotChatList.size(); i++) {
                BotMethodSwitch.chatHashMap.put(workBotChatList.get(i).getKeyword(), workBotChatList.get(i).getResponse());
            }
        }

    }

    public void updateBlackKeyList(List<BlackKeyList> blackKeyLists) {
        if (blackKeyLists != null && blackKeyLists.size() >= 1) {
            for (int i = 0; i < blackKeyLists.size(); i++) {
                BotMethodSwitch.blackList.add(blackKeyLists.get(i).getKeyword());
            }
        }
    }

    public void updateWhiteKeyList(List<WhiteKeyList> whiteKeyLists) {
        if (whiteKeyLists != null && whiteKeyLists.size() >= 1) {
            for (int i = 0; i < whiteKeyLists.size(); i++) {
                BotMethodSwitch.whiteList.add(whiteKeyLists.get(i).getKeyword());
            }
        }
    }

    //18个开关
    public void initHashMap(){
        BotMethodSwitch.botMethod.put("deletePhoto",false);
        BotMethodSwitch.botMethod.put("deleteFile",false);
        BotMethodSwitch.botMethod.put("deleteVoice",false);
        BotMethodSwitch.botMethod.put("deleteVideo",false);
        BotMethodSwitch.botMethod.put("deleteForward",false);
        BotMethodSwitch.botMethod.put("deleteJoinOutMsg",false);
        BotMethodSwitch.botMethod.put("deleteSendLink",false);
        BotMethodSwitch.botMethod.put("deleteLongUsername",false);
        BotMethodSwitch.botMethod.put("kitManWhoDeleteThreeMsg",false);
        BotMethodSwitch.botMethod.put("useTuling",false);
        BotMethodSwitch.botMethod.put("disableAddBot",false);
        BotMethodSwitch.botMethod.put("disableSpam",false);
        BotMethodSwitch.botMethod.put("deleteAdKeyFlag",false);
        BotMethodSwitch.botMethod.put("useWelcome",false);
        BotMethodSwitch.botMethod.put("useWhiteList",false);
        BotMethodSwitch.botMethod.put("useChat",false);
        BotMethodSwitch.botMethod.put("useSendMsgOnTime",false);
        BotMethodSwitch.botMethod.put("useToken",false);
        BotMethodSwitch.botMethod.put("useTokenSay",false);
        BotMethodSwitch.botMethod.put("useKitMan",false);
        BotMethodSwitch.botMethod.put("useDeleteMsg",false);
        BotMethodSwitch.botMethod.put("useRedPaper",false);
        BotMethodSwitch.botMethod.put("useGuessing",false);
        BotMethodSwitch.botMethod.put("useCheckIn",false);
        BotMethodSwitch.botMethod.put("useChatToken",false);
    }
}
